package net.xju3_4.dao;

import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.entity.Venues;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class VenuesDao {
    public List<Venues> list() {
        // 操作数据库
        // 获取连接
        Connection connection = JdbcUtilsV1.getConnection();
        String sql = "select * from venues";
        List<Venues> venuesList = JdbcUtilsV1.executeQuery(connection, Venues.class, sql);
        return venuesList;
    }

    public void insert(Venues venues) {
        String sql = "INSERT INTO venues(venue_Name,location,capacity,venue_Type,base_Price,available_hours," +
                "min_Rental_Hours,status,description,images,created_At,updated_At) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
        JdbcUtilsV1.executeUpdate(sql, venues.getVenueName(),
                venues.getLocation(),venues.getCapacity(),venues.getVenueType(),venues.getBasePrice(),venues.getAvailablehours(),
                venues.getMinRentalHours(),venues.getStatus(),venues.getDescription(),venues.getImages(),
                venues.getCreatedAt(),venues.getUpdatedAt());
    }

    public Venues selectOne(int id) {
        String sql = "select id,venue_Name venueName,location,capacity,venue_Type venueType,base_Price basePrice,available_hours availablehours," +
                " min_Rental_Hours minRentalHours,status ,description ,images,created_At createdAt,updated_At updatedAt" +
                " from venues where id=?";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(), Venues.class, sql, id);
    }

    public void deleteById(int id) {
        // 逻辑删除，实际执行update
        String sql = "delete from venues where id = ?";
        JdbcUtilsV1.executeUpdate(sql, id);
    }

    public void update(Venues venues) {
        StringBuilder sql = new StringBuilder("UPDATE venues SET ");
        List<Object> params = new ArrayList<>();

        // 动态拼接非空字段
        if (venues.getVenueName() != null &&!venues.getVenueName().isEmpty()) {
            sql.append( "venue_Name = ?, ");
            params.add(venues.getVenueName());
        }

        if (venues.getLocation() != null &&!venues.getLocation().isEmpty()) {
            sql.append( "location = ?, ");
            params.add(venues.getLocation());
        }
        if (venues.getCapacity() != null&&venues.getCapacity() != 0) {
            sql.append( "capacity = ?, ");
            params.add(venues.getCapacity());
        }
        if (venues.getVenueType() != null&&!venues.getVenueType().isEmpty()) {
            sql.append( "venue_Type = ?, ");
            params.add(venues.getVenueType());
        }
        if (venues.getBasePrice() != null&&venues.getBasePrice() != 0.0) {
            sql.append( "base_Price = ?, ");
            params.add(venues.getBasePrice());
        }
        if (venues.getAvailablehours() != null&&!venues.getAvailablehours().isEmpty()) {
            sql.append( "available_hours = ?, ");
            params.add(venues.getAvailablehours());
        }
        if (venues.getMinRentalHours() != null&&venues.getMinRentalHours() != 0) {
            sql.append( "min_Rental_Hours = ?, ");
            params.add(venues.getMinRentalHours());
        }
        if (venues.getStatus() != null&&!venues.getStatus().isEmpty()) {
            sql.append( "status = ?, ");
            params.add(venues.getStatus());
        }
        if (venues.getDescription() != null&&!venues.getDescription().isEmpty()) {
            sql.append( "description = ?, ");
            params.add(venues.getDescription());
        }
        if (venues.getImages() != null&&!venues.getImages().isEmpty()) {
            sql.append( "images = ?, ");
            params.add(venues.getImages());
        }
        if (venues.getCreatedAt() != null) {
            sql.append( "created_At = ?, ");
            params.add(venues.getCreatedAt());
        }
        if (venues.getUpdatedAt() != null) {
            sql.append( "updated_At = ?, ");
            params.add(venues.getUpdatedAt());
        }
        // 移除最后的逗号和空格
        if (!params.isEmpty()) {
            sql.delete(sql.length() - 2, sql.length());
        }
        // 添加WHERE条件
        sql.append(" WHERE id = ?");
        params.add(venues.getId());
        JdbcUtilsV1.executeUpdate(sql.toString(),params.toArray());
    }

    public PageInfo<Venues> pageList(String venueName, String venueType, Integer pageNo, Integer pageSize) {
        String sql = "select id,venue_Name venueName,location,capacity,venue_Type venueType,base_Price basePrice,available_hours availablehours," +
                " min_Rental_Hours minRentalHours,status ,description ,images,created_At createdAt,updated_At updatedAt" +
                " from venues where 1 = 1 ";
        List<Object> params = new ArrayList<Object>();
        // 拼接sql 条件查询
        if (venueName != null && !venueName.isEmpty()) {
            sql += " and venue_Name like ?";
            params.add("%" + venueName + "%");
        }
        if (venueType != null && !venueType.isEmpty()) {
            sql += " and venue_Type = ?";
            params.add(venueType);
        }
        sql += " limit ?,?";
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        List<Venues> venuess = JdbcUtilsV1.executeQuery(Venues.class, sql, params.toArray());
        // 分页
        String countSql = "select count(*) from venues where 1 = 1 ";
        long count = JdbcUtilsV1.executeQueryOneValue(countSql);
        PageInfo<Venues> pageInfo = new PageInfo<>(pageNo, pageSize, (int) count, venuess);
        return pageInfo;
    }
}
